package com.yuncheng.spcyApi.partyService;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.entity.YcSysUser;
import com.yuncheng.spcyApi.bo.PartyHyCjryParamBo;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.*;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.spcyApi.mapper.PartyRsDwhyjlMapper;
import com.yuncheng.spcyApi.scheduleJob.ScheduleJobService;
import com.yuncheng.spcyApi.service.*;
import com.yuncheng.spcyApi.spcyService.ZdyUploadFileService;
import com.yuncheng.spcyApi.utils.*;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.spcyApi.vo.sqsx.HyPrintVo;
import com.yuncheng.spcyApi.vo.common.OrderItemVo;
import com.yuncheng.spcyApi.vo.common.StartEndStringVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import utils.DateConversionUtils;
import utils.LogUtils;
import utils.ObjectToMapUtils;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * 党务会议
 */
@Component(value = "partyHyService")
public class PartyHyService {

    private static final Logger log = LoggerFactory.getLogger(PartyHyService.class);

    @Resource
    @Lazy
    private PartyRsDwhyjlMapper partyRsDwhyjlMapper;

    @Resource
    @Lazy
    private IPartyRsDwhyjhService partyRsDwhyjhService;

    @Resource
    @Lazy
    private IPartyRsDwhyjhcjryService partyRsDwhyjhcjryService;

    @Resource
    @Lazy
    private IPartyRsDwhyjlService partyRsDwhyjlService;

    @Resource
    @Lazy
    private IPartyRsDwhycjryService partyRsDwhycjryService;

    @Resource
    @Lazy
    private IYcSysUserService sysUserService;

    @Resource
    @Lazy
    private ZdyUploadFileService zdyUploadFileService;

    @Resource
    @Lazy
    private ScheduleJobService scheduleJobService;

    @Resource
    @Lazy
    private ISpcyLogService spcyLogService;

    /************************************[ 会议计划 ]**************************************************************************/

    /**
     * 党务会议计划-分页查询
     * @return
     */
    public HttpResult queryHyjhPageList(PartyRsDwhyjh query,Integer pageNo,Integer pageSize,OrderItemVo orderItemVo){
        Page<PartyRsDwhyjh> page = new Page<>(pageNo,pageSize);

        LambdaQueryWrapper<PartyRsDwhyjh> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFhydd()), PartyRsDwhyjh::getFhydd, query.getFhydd());
        queryWrapper.like(StringUtils.isNotBlank(query.getFhybt()), PartyRsDwhyjh::getFhybt, query.getFhybt());
        queryWrapper.like(StringUtils.isNotBlank(query.getFhyzy()), PartyRsDwhyjh::getFhyzy, query.getFhyzy());
        queryWrapper.like(StringUtils.isNotBlank(query.getFyxzt()), PartyRsDwhyjh::getFyxzt, query.getFyxzt());
        queryWrapper.like(StringUtils.isNotBlank(query.getFhylx()), PartyRsDwhyjh::getFhylx, query.getFhylx());

        if (StringUtils.isBlank(orderItemVo.getOrderItem())) {
            queryWrapper.orderByDesc(PartyRsDwhyjh::getCreateTime);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        IPage<PartyRsDwhyjh> pageList = partyRsDwhyjhService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    /**
     * 党务会议计划-添加
     */
    public PartyRsDwhyjh addHyjh(PartyRsDwhyjh hyjh){
        hyjh.setId(GetUuIdUtils.ReplaceUuId());

        if (hyjh.getFhyjgts() == null){
            hyjh.setFhyjgts(0);
        }
        if (StringUtils.isBlank(hyjh.getFschykssj())){
            throw new BusinessException("请填写首次会议时间！");
        }

        if (StringUtils.isBlank(hyjh.getFyxzt())){
            hyjh.setFyxzt(SpcyCommonConstant.HYJH_YXZT_NO);
        }

        if (StringUtils.isBlank(hyjh.getFsfxhzx())){
            hyjh.setFsfxhzx(SpcyCommonConstant.HYJH_SFXHZX_NO);
        }

        if (StringUtils.isBlank(hyjh.getFhyjlry())){
            throw new BusinessException("请选择记录人员!");
        }
        List<String> idsList = StringPatternUtils.getListByIds(hyjh.getFhyjlry());

        List<YcSysUser> userList = sysUserService.listByIds(idsList);
        String hyjlryxm = "";
        for (YcSysUser user : userList){
            if (StringUtils.isBlank(hyjlryxm)){
                hyjlryxm += user.getRealname();
            }else {
                hyjlryxm += "/"+user.getRealname();
            }
        }
        hyjh.setFhyjlryxm(hyjlryxm);

        partyRsDwhyjhService.save(hyjh);

        scheduleJobService.initScheduleDwhyjh(partyRsDwhyjhService.getById(hyjh.getId()));

        String log = "添加-党务会议计划：";
        log += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(hyjh));
        spcyLogService.addLogs(LogConstant.LOGS_PARTY_DWHY,log, hyjh.getId(), "添加-党务会议计划",SpcyConstant.LOGS_SYS_TYPE_XZ);

        return hyjh;
    }

    /**
     * 修改党务会议记录
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public PartyRsDwhyjh updateHyjh(HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        if (StringUtils.isEmpty(buffer.get("id"))){
            throw new BusinessException("操作失败，数据参数为空！");
        }
        String id = buffer.get("id");
        PartyRsDwhyjh updateBean = partyRsDwhyjhService.getById(id);
        if (updateBean == null){
            throw new BusinessException("操作失败，没有相匹配的数据！");
        }

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);

        if (StringUtils.isBlank(updateBean.getFschykssj())){
            throw new BusinessException("请填写首次会议时间！");
        }

        if (StringUtils.isBlank(updateBean.getFhyjlry())){
            throw new BusinessException("请选择记录人员!");
        }
        List<String> idsList = StringPatternUtils.getListByIds(updateBean.getFhyjlry());

        List<YcSysUser> userList = sysUserService.listByIds(idsList);
        String hyjlryxm = "";
        for (YcSysUser user : userList){
            if (StringUtils.isBlank(hyjlryxm)){
                hyjlryxm += user.getRealname();
            }else {
                hyjlryxm += "/"+user.getRealname();
            }
        }
        updateBean.setFhyjlryxm(hyjlryxm);

        partyRsDwhyjhService.updateById(updateBean);

        String title = "修改党务会议计划信息";
        String operContent = title +": "+ LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_PARTY_DWHY, operContent, updateBean.getId(),title, SpcyConstant.LOGS_SYS_TYPE_XG);

        scheduleJobService.initScheduleDwhyjh(partyRsDwhyjhService.getById(id));

        return updateBean;
    }

    /**
     * 删除党务会议计划
     * @param id
     */
    public void deleteHyjh(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("数据参数为空！");
        }
        PartyRsDwhyjh bean = partyRsDwhyjhService.getById(id);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }
        partyRsDwhyjhService.removeById(id);
        scheduleJobService.pauseJobAndDeleteByParam(id);

        String log = "删除-党务会议计划：";
        log += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_PARTY_DWHY,log, bean.getId(), "删除-党务会议计划",SpcyConstant.LOGS_SYS_TYPE_SC);
    }

    /**
     * 查询会议计划参加人员
     */
    public List<PartyRsDwhyjhcjry> queryHyjhCjryList(String fhyid){
        if (StringUtils.isBlank(fhyid)){
            throw new BusinessException("会议参数为空！");
        }
        List<PartyRsDwhyjhcjry> list = partyRsDwhyjhcjryService.list(new LambdaQueryWrapper<PartyRsDwhyjhcjry>()
                .eq(PartyRsDwhyjhcjry::getFhyid, fhyid));

        return list;
    }

    /**
     * 会议计划参加人员-添加
     */
    public void addHyjhCjry(PartyHyCjryParamBo paramBo){
        if (StringUtils.isBlank(paramBo.getFhyid())){
            throw new BusinessException("会议数据参数为空！");
        }
        if (StringUtils.isBlank(paramBo.getFuserid())){
            throw new BusinessException("人员参数为空！");
        }
        List<String> idsList = StringPatternUtils.getListByIds(paramBo.getFuserid());

        List<YcSysUser> userList = sysUserService.listByIds(idsList);

        List<PartyRsDwhyjhcjry> cjryList = new ArrayList<>();
        PartyRsDwhyjhcjry cjry = null;
        for (YcSysUser user : userList){
            cjry = new PartyRsDwhyjhcjry();

            // 是否存在人员
            List<PartyRsDwhyjhcjry> existsUser = partyRsDwhyjhcjryService.list(new LambdaQueryWrapper<PartyRsDwhyjhcjry>()
                    .eq(PartyRsDwhyjhcjry::getFhyid, paramBo.getFhyid())
                    .eq(PartyRsDwhyjhcjry::getFuserid, user.getId()));
            if (CollectionUtil.isNotEmpty(existsUser)){
                continue;
            }

            cjry.setId(GetUuIdUtils.ReplaceUuId());
            cjry.setFhyid(paramBo.getFhyid());
            cjry.setFuserid(user.getId());
            cjry.setFxm(user.getRealname());

            cjryList.add(cjry);
        }

        partyRsDwhyjhcjryService.saveBatch(cjryList);
    }

    /**
     * 会议计划参加人员-删除
     */
    public void deleteHjjhCjry(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("数据参数为空！");
        }
        PartyRsDwhyjhcjry cjry = partyRsDwhyjhcjryService.getById(id);
        if (cjry == null){
            throw new BusinessException("没有想匹配的数据！");
        }
        partyRsDwhyjhcjryService.removeById(id);

        String log = "删除-党务会议计划参加人员：";
        log += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(cjry));
        spcyLogService.addLogs(LogConstant.LOGS_PARTY_DWHY,log, cjry.getId(), "删除-党务会议计划",SpcyConstant.LOGS_SYS_TYPE_SC);
    }

    /**
     * 会议计划-同步会议
     * @param fhyjhid 会议计划id
     */
    public void syncHyjl(PartyRsDwhyjh jh,String fhyjhid){
        if (StringUtils.isBlank(fhyjhid)){
            log.error("会议计划-同步会议失败: 会议计划id为空。");
            return;
        }

        if (jh == null) {
            jh = partyRsDwhyjhService.getById(fhyjhid);
        }

        if (jh == null){
            log.error("会议计划-同步会议失败: 记录为空,会议计划id为{}", fhyjhid);
            return;
        }

        PartyRsDwhyjl hy = new PartyRsDwhyjl();
        try {
            BeanUtils.copyProperties(hy, jh);
        } catch (IllegalAccessException e) {
            log.error("会议计划-同步会议失败：【IllegalAccessException】-》{}",e.getMessage());
        } catch (InvocationTargetException e) {
            log.error("会议计划-同步会议失败：【InvocationTargetException】-》{}",e.getMessage());
        }
        hy.setId(GetUuIdUtils.ReplaceUuId());
        hy.setFhyzt(SpcyCommonConstant.HYJL_HYZT_WKS);

        String newHysj = ""; // 新会议时间
        String jhsj = jh.getFschykssj();

        // yyyy-MM-dd HH:*** 格式日期 截取时分秒
        String sfmTime = jhsj.substring(10, jhsj.length());

        // 当前年月日
        String nowYmdTime = DateConversionUtils.DateToStringYYYYMMDD(new Date());
        // 组合新的时间
        newHysj = nowYmdTime + sfmTime;

        hy.setFhysj(newHysj);

        hy.setCreateTime(new Date());
        hy.setUpdateTime(new Date());

        List<PartyRsDwhyjl> list = partyRsDwhyjlService.list(new LambdaQueryWrapper<PartyRsDwhyjl>()
                .eq(PartyRsDwhyjl::getFhyjlry, hy.getFhyjlry())
                .eq(PartyRsDwhyjl::getFhysj, hy.getFhysj())
                .eq(PartyRsDwhyjl::getFhybt, hy.getFhybt()));
        if (CollectionUtil.isEmpty(list)) {
            List<PartyRsDwhyjhcjry> jhRyList = queryHyjhCjryList(fhyjhid);
            int cjrySize = jhRyList.size();

            if (StringUtils.isNotBlank(hy.getFhyjlry())) {
                List<String> jlryList = StringPatternUtils.getListByIds(hy.getFhyjlry());
                cjrySize += jlryList.size();
            }
            hy.setFydrs(cjrySize);
            hy.setFsdrs(0);

            partyRsDwhyjlService.save(hy);
            syncHyjlCjry(jhRyList, hy.getId());
        }
    }

    /**
     * 会议计划-同步会议参加人员
     * @param fhyid 会议id
     */
    public void syncHyjlCjry(List<PartyRsDwhyjhcjry> jhRyList, String fhyid){
        if (CollectionUtil.isEmpty(jhRyList)){
            return;
        }

        List<PartyRsDwhycjry> cjryList = new ArrayList<>();
        PartyRsDwhycjry hyRy = null;
        for (PartyRsDwhyjhcjry jhry : jhRyList){
            hyRy = new PartyRsDwhycjry();

            try {
                BeanUtils.copyProperties(hyRy, jhry);
            } catch (IllegalAccessException e) {
                log.error("会议计划-同步会议参加人员失败：【IllegalAccessException】-》{}",e.getMessage());
            } catch (InvocationTargetException e) {
                log.error("会议计划-同步会议参加人员失败：【InvocationTargetException】-》{}",e.getMessage());
            }
            hyRy.setId(GetUuIdUtils.ReplaceUuId());
            hyRy.setFhyid(fhyid);
            hyRy.setCreateTime(new Date());
            hyRy.setUpdateTime(new Date());
            hyRy.setFqjzt(SpcyCommonConstant.HYJL_CJRY_ZT_ZC);

            cjryList.add(hyRy);
        }

        partyRsDwhycjryService.saveBatch(cjryList);
    }

    /************************************[ 会议记录]**************************************************************************/

    /**
     * 党务会议记录-分页查询
     * @return
     */
    public HttpResult queryHyjlPageList(PartyRsDwhyjl query,
                                        StartEndStringVo timeVo,
                                        Integer pageNo, Integer pageSize,
                                        OrderItemVo orderItemVo){
        Page<PartyRsDwhyjl> page = new Page<>(pageNo,pageSize);

        LambdaQueryWrapper<PartyRsDwhyjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(query.getFhydd()), PartyRsDwhyjl::getFhydd, query.getFhydd());
        queryWrapper.like(StringUtils.isNotBlank(query.getFhybt()), PartyRsDwhyjl::getFhybt, query.getFhybt());
        queryWrapper.like(StringUtils.isNotBlank(query.getFhyzy()), PartyRsDwhyjl::getFhyzy, query.getFhyzy());
        queryWrapper.like(StringUtils.isNotBlank(query.getFhynr()), PartyRsDwhyjl::getFhynr, query.getFhynr());
        queryWrapper.like(StringUtils.isNotBlank(query.getFhylx()), PartyRsDwhyjl::getFhylx, query.getFhylx());
        queryWrapper.like(StringUtils.isNotBlank(query.getFhyjlry()), PartyRsDwhyjl::getFhyjlry, query.getFhyjlry());
        queryWrapper.like(StringUtils.isNotBlank(query.getFhyjlryxm()), PartyRsDwhyjl::getFhyjlryxm, query.getFhyjlryxm());
        queryWrapper.like(StringUtils.isNotBlank(query.getFhyzt()), PartyRsDwhyjl::getFhyzt, query.getFhyzt());

        if (StringUtils.isNotBlank(timeVo.getStartTime()) && StringUtils.isBlank(timeVo.getEndTime())){
            queryWrapper.apply("fhysj >= {0}",timeVo.getStartTime());
        }else if (StringUtils.isBlank(timeVo.getStartTime()) && StringUtils.isNotBlank(timeVo.getEndTime())){
            queryWrapper.apply("fhysj <= {0}", timeVo.getEndTime());
        }else if (StringUtils.isNotBlank(timeVo.getStartTime()) && StringUtils.isNotBlank(timeVo.getEndTime())){
            queryWrapper.apply("fhysj >= {0} and fhysj <= {1}",timeVo.getStartTime(), timeVo.getEndTime());
        }

        if (StringUtils.isBlank(orderItemVo.getOrderItem())) {
            queryWrapper.orderByDesc(PartyRsDwhyjl::getCreateTime);
        }else {
            OrderItemUtils.setOrderItem(queryWrapper, orderItemVo);
        }
        IPage<PartyRsDwhyjl> pageList = partyRsDwhyjlService.page(page, queryWrapper);

        return HttpResult.ok(pageList);
    }

    /**
     * 党务会议记录-分页查询
     * @return
     */
    public HttpResult queryMyHyjlPageList(PartyRsDwhyjl query,StartEndStringVo timeVo,Integer pageNo,Integer pageSize,OrderItemVo orderItemVo){
        Page<PartyRsDwhyjl> page = new Page<>(pageNo,pageSize);

        IPage<PartyRsDwhyjl> pageList = partyRsDwhyjlMapper.findMyHyjlPageList(page, timeVo, query, GetCurrentUserUtils.getCurrentUserId(), orderItemVo);

        return HttpResult.ok(pageList);
    }

    /**
     * 党务会议记录-添加
     */
    public PartyRsDwhyjl addHyjl(PartyRsDwhyjl hyjl){
        hyjl.setId(GetUuIdUtils.ReplaceUuId());

        if (StringUtils.isBlank(hyjl.getFhysj())){
            throw new BusinessException("请填写会议时间！");
        }

        if (StringUtils.isBlank(hyjl.getFhyjlry())){
            throw new BusinessException("请选择记录人员!");
        }
        List<String> idsList = StringPatternUtils.getListByIds(hyjl.getFhyjlry());

        List<YcSysUser> userList = sysUserService.listByIds(idsList);
        String hyjlryxm = "";
        for (YcSysUser user : userList){
            if (StringUtils.isBlank(hyjlryxm)){
                hyjlryxm += user.getRealname();
            }else {
                hyjlryxm += "/"+user.getRealname();
            }
        }
        hyjl.setFhyjlryxm(hyjlryxm);

        hyjl.setFhyzt(SpcyCommonConstant.HYJL_HYZT_WKS);

        if (hyjl.getFydrs() == null) {
            hyjl.setFydrs(0);
        }
        hyjl.setFsdrs(0);

        partyRsDwhyjlService.save(hyjl);

        String log = "添加-党务会议记录：";
        log += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(hyjl));
        spcyLogService.addLogs(LogConstant.LOGS_PARTY_DWHY,log, hyjl.getId(), "添加-党务会议记录",SpcyConstant.LOGS_SYS_TYPE_XZ);

        return hyjl;
    }

    /**
     * 修改党务会议记录
     * @throws InvocationTargetException
     * @throws IllegalAccessException
     */
    public PartyRsDwhyjl updateHyjl(HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException {
        if (StringUtils.isEmpty(buffer.get("id"))){
            throw new BusinessException("操作失败，数据参数为空！");
        }
        String wjmlId = buffer.get("id");
        PartyRsDwhyjl updateBean = partyRsDwhyjlService.getById(wjmlId);
        if (updateBean == null){
            throw new BusinessException("操作失败，没有相匹配的数据！");
        }

        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(updateBean);
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(updateBean, buffer);

        if (StringUtils.isBlank(updateBean.getFhysj())){
            throw new BusinessException("请填写会议时间！");
        }

        if (StringUtils.isBlank(updateBean.getFhyjlry())){
            throw new BusinessException("请选择记录人员!");
        }
        List<String> idsList = StringPatternUtils.getListByIds(updateBean.getFhyjlry());

        List<YcSysUser> userList = sysUserService.listByIds(idsList);
        String hyjlryxm = "";
        for (YcSysUser user : userList){
            if (StringUtils.isBlank(hyjlryxm)){
                hyjlryxm += user.getRealname();
            }else {
                hyjlryxm += "/"+user.getRealname();
            }
        }
        updateBean.setFhyjlryxm(hyjlryxm);

        partyRsDwhyjlService.updateById(updateBean);

        String title = "修改党务会议信息";
        String operContent = title +": "+ LogUtils.BeanModifyMsg(updateBean,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_PARTY_DWHY, operContent, updateBean.getId(),title, SpcyConstant.LOGS_SYS_TYPE_XG);

        return updateBean;
    }



    /**
     * 删除会议记录
     * @param id
     */
    public void deleteHyjl(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("数据参数为空！");
        }
        PartyRsDwhyjl bean = partyRsDwhyjlService.getById(id);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }
        partyRsDwhyjlService.removeById(id);

        String log = "删除-党务会议记录：";
        log += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(bean));
        spcyLogService.addLogs(LogConstant.LOGS_PARTY_DWHY,log, bean.getId(), "删除-党务会议记录",SpcyConstant.LOGS_SYS_TYPE_SC);

    }

    /**
     * 会议记录详情
     * @return
     * {
     *     hy: 会议信息
     *     fjList: 附件记录
     *     cjryList: 参加人员
     * }
     */
    public Map queryHyxqById(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("数据参数为空！");
        }
        PartyRsDwhyjl hy = partyRsDwhyjlService.getById(id);

        Map result = new HashMap();
        result.put("hy", hy);
        result.put("fjList", zdyUploadFileService.queryWjjlByPid(id));
        result.put("cjryList", queryHyjlCjryList(id));
        result.put("qjryList", queryHyjlQjryList(id));

        return result;
    }

    /**
     * 完成会议记录
     */
    public void completionHyjl(String id, String fhynr, PartyRsDwhyjl hy){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("数据参数为空！");
        }
        PartyRsDwhyjl bean = partyRsDwhyjlService.getById(id);
        if (bean == null){
            throw new BusinessException("没有相匹配的数据！");
        }
        boolean isEmpty = false;
        if (StringUtils.isBlank(bean.getFhynr())) {
            isEmpty = true;
        }

        if (isEmpty){
            bean.setFhynr(fhynr);
        }

        if (!isEmpty && StringUtils.isNotBlank(fhynr)){
            bean.setFhynr(fhynr);
        }

        bean.setFhyzt(SpcyCommonConstant.HYJL_HYZT_YWC);

        bean.setFydrs(hy.getFydrs());
        bean.setFsdrs(hy.getFsdrs());
        partyRsDwhyjlService.updateById(bean);
    }

    /**
     * 查询会议记录参加人员
     */
    public List<PartyRsDwhycjry> queryHyjlCjryList(String fhyid){
        if (StringUtils.isBlank(fhyid)){
            throw new BusinessException("会议参数为空！");
        }
        List<PartyRsDwhycjry> list = partyRsDwhycjryService.list(new LambdaQueryWrapper<PartyRsDwhycjry>()
                .eq(PartyRsDwhycjry::getFhyid, fhyid)
                .eq(PartyRsDwhycjry::getFqjzt, SpcyCommonConstant.HYJL_CJRY_ZT_ZC));

        return list;
    }

    /**
     * 查询会议记录请假人员
     */
    public List<PartyRsDwhycjry> queryHyjlQjryList(String fhyid){
        if (StringUtils.isBlank(fhyid)){
            throw new BusinessException("会议参数为空！");
        }
        List<PartyRsDwhycjry> list = partyRsDwhycjryService.list(new LambdaQueryWrapper<PartyRsDwhycjry>()
                .eq(PartyRsDwhycjry::getFhyid, fhyid)
                .eq(PartyRsDwhycjry::getFqjzt, SpcyCommonConstant.HYJL_CJRY_ZT_QJ));

        return list;
    }

    /**
     * 会议记录参加人员-添加
     */
    public void addHyjlCjry(PartyHyCjryParamBo paramBo){
        if (StringUtils.isBlank(paramBo.getFhyid())){
            throw new BusinessException("会议数据参数为空！");
        }
        if (StringUtils.isBlank(paramBo.getFuserid())){
            throw new BusinessException("人员参数为空！");
        }
        List<String> idsList = StringPatternUtils.getListByIds(paramBo.getFuserid());

        List<YcSysUser> userList = sysUserService.listByIds(idsList);

        List<PartyRsDwhycjry> cjryList = new ArrayList<>();
        PartyRsDwhycjry cjry = null;
        for (YcSysUser user : userList){
            cjry = new PartyRsDwhycjry();

            // 是否存在人员
            List<PartyRsDwhycjry> existsUser = partyRsDwhycjryService.list(new LambdaQueryWrapper<PartyRsDwhycjry>()
                    .eq(PartyRsDwhycjry::getFhyid, paramBo.getFhyid())
                    .eq(PartyRsDwhycjry::getFuserid, user.getId())
                    .eq(PartyRsDwhycjry::getFqjzt, SpcyCommonConstant.HYJL_CJRY_ZT_ZC));
            if (CollectionUtil.isNotEmpty(existsUser)){
                continue;
            }

            cjry.setId(GetUuIdUtils.ReplaceUuId());
            cjry.setFhyid(paramBo.getFhyid());
            cjry.setFuserid(user.getId());
            cjry.setFxm(user.getRealname());
            cjry.setFqjzt(SpcyCommonConstant.HYJL_CJRY_ZT_ZC);

            cjryList.add(cjry);
        }

        partyRsDwhycjryService.saveBatch(cjryList);
    }

    /**
     * 会议记录请假人员-添加
     */
    public void addHyjlQjry(PartyHyCjryParamBo paramBo){
        if (StringUtils.isBlank(paramBo.getFhyid())){
            throw new BusinessException("会议数据参数为空！");
        }
        if (StringUtils.isBlank(paramBo.getFuserid())){
            throw new BusinessException("人员参数为空！");
        }
        List<String> idsList = StringPatternUtils.getListByIds(paramBo.getFuserid());

        List<YcSysUser> userList = sysUserService.listByIds(idsList);

        List<PartyRsDwhycjry> cjryList = new ArrayList<>();
        PartyRsDwhycjry cjry = null;
        for (YcSysUser user : userList){
            cjry = new PartyRsDwhycjry();

            // 是否存在人员
            List<PartyRsDwhycjry> existsUser = partyRsDwhycjryService.list(new LambdaQueryWrapper<PartyRsDwhycjry>()
                    .eq(PartyRsDwhycjry::getFhyid, paramBo.getFhyid())
                    .eq(PartyRsDwhycjry::getFuserid, user.getId())
                    .eq(PartyRsDwhycjry::getFqjzt, SpcyCommonConstant.HYJL_CJRY_ZT_QJ));
            if (CollectionUtil.isNotEmpty(existsUser)){
                continue;
            }

            cjry.setId(GetUuIdUtils.ReplaceUuId());
            cjry.setFhyid(paramBo.getFhyid());
            cjry.setFuserid(user.getId());
            cjry.setFxm(user.getRealname());
            cjry.setFqjzt(SpcyCommonConstant.HYJL_CJRY_ZT_QJ);

            cjryList.add(cjry);
        }

        partyRsDwhycjryService.saveBatch(cjryList);
    }

    /**
     * 会议记录参加人员-删除
     */
    public void deleteHjjlCjry(String id){
        if (StringUtils.isBlank(id)){
            throw new BusinessException("数据参数为空！");
        }
        PartyRsDwhycjry cjry = partyRsDwhycjryService.getById(id);
        if (cjry == null){
            throw new BusinessException("没有想匹配的数据！");
        }
        partyRsDwhycjryService.removeById(id);

        String log = "删除-党务会议参加人员：";
        log += LogUtils.MapToStringMethod(ObjectToMapUtils.objectToMap(cjry));
        spcyLogService.addLogs(LogConstant.LOGS_PARTY_DWHY,log, cjry.getId(), "删除-党务会议参加人员",SpcyConstant.LOGS_SYS_TYPE_SC);
    }

    /**
     * 上传文件
     * @param uploadFile
     * @param fpid 业务id
     */
    public void uploadFile(MultipartFile[] uploadFile, String fpid){
        zdyUploadFileService.uploadFile(uploadFile, fpid, LogConstant.LOGS_PARTY_DWHY, SpcyConstant.PARTY_HY_FILE_BASE_URL+"/"+DateConversionUtils.DateToMonthTwo(new Date()));
    }

    /**
     * 打印列表数据
     */
    public List<Map> queryPrintDataList(HyPrintVo printVo){
        List<PartyRsDwhyjl> hyList = new ArrayList<>();
        if (printVo.getIsPrintAll().equals("是")){
            hyList = partyRsDwhyjlMapper.findMyHyjlList(GetCurrentUserUtils.getCurrentUserId());

        }else {
            if (StringUtils.isBlank(printVo.getIds())){
                throw new BusinessException("请选择数据！");
            }
            List<String> idList = StringPatternUtils.getListByIds(printVo.getIds());
             hyList = partyRsDwhyjlService.listByIds(idList);
        }

        List<Map> resultMapList = new ArrayList<>();
        Map dataMap = null;
        for (PartyRsDwhyjl hy : hyList){
            dataMap = new HashMap();

            dataMap.put("hy", hy);
            dataMap.put("cjryList", queryHyjlCjryList(hy.getId()));
            dataMap.put("qjryList", queryHyjlQjryList(hy.getId()));

            resultMapList.add(dataMap);
        }


        return resultMapList;
    }


}
